﻿layui.use(['form', 'layedit', 'layer', 'laydate', 'jquery'], function () {

    var form = layui.form, layer = parent.layer === undefined ? layui.layer : top.layer, $ = layui.jquery, player = parent.layer || layui.layer;

    var adminForgetPswLogic = {
        //初始化
        init: function () {
            self = this;
            ///全局参数
            this.Parameters = {
                sendMessageUrl: "/Admin/Login/ForgetPswSendMessage",
                ResetPasswordUrl: "/Admin/Login/ResetPassword",
            };
            //页面初始化
            this.initPage();
            //时间初始化
            this.bindEvent();
        },
        //初始化列表
        initPage: function () {

        },
        //绑定事件
        bindEvent: function () {

            //倒计时
            $('#ValidCode').on('click', function () {
                var _this = $(this);
                self.logicFunc.sendMessage(_this);
            });

            //重置密码
            form.on('submit(modifyButton)', self.logicFunc.resetPassword);

            $(document).keydown(function (event) {
                if (event.keyCode == 13) {
                    self.logicFunc.resetPassword();
                }
            });
        },
        //事件
        logicFunc: {
            //发送短信息
            sendMessage: function (_this) {

                if (!$(_this).hasClass("on")) {
                    var jsonPar = GrdLayUI.initParamsData($("#add-form"))
                    if (!GrdJudge.isMobilePhone(jsonPar.sLoginName)) {
                        alert("手机号码格式不对！");
                        return;
                    }
                    var json = {
                        parameters: jsonPar.sLoginName
                    };
                    GrdLayUI.ajaxPost({
                        url: self.Parameters.sendMessageUrl,
                        data: json,
                        success: function (result) {
                            if (result.Code == 1000) {
                                $.leftTime(60, function (d) {
                                    if (d.status) {
                                        _this.addClass("on");
                                        _this.html((d.s == "00" ? "60" : d.s) + "秒后再获取");
                                    } else {
                                        _this.removeClass("on");
                                        _this.html("获取验证码");
                                    }
                                });
                            } else {
                                GrdLayUI.msg(result.Msg, "警告");
                            }
                        }
                    });
                } else {
                    GrdLayUI.msg("请倒计时结束后获取！", "警告");
                }
            },

            //重置密码
            resetPassword: function () {
                var jsonPar = GrdLayUI.initParamsData($("#add-form"))
                var json = {
                    parameters: jsonPar
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.ResetPasswordUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            GrdLayUI.msg(result.Msg, "成功", function () {
                                //获取窗口索引
                                var index = player.getFrameIndex(window.name);
                                //关闭子页面
                                player.close(index);
                            });
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            }
        },
        //工具
        tools: {

        }
    };
    adminForgetPswLogic.init();
});

